package com.jams.goodox.activity.mine;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangzhou.jin.customview.SpaceItemDecoration;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.home.ColtResp;
import com.jams.goodox.activity.home.GoodDetailActivity;
import com.jams.goodox.entity.BaokuanEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.fresco.FrescoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/1.
 */

public class ProductInActivity extends BaseActivity{

    @Bind(R.id.recycle_view)
    RecyclerView recyclerView;

    private List<BaokuanEntity> cnxhEntityList;
    private BaseQuickAdapter baseQuickAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.product_in_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this,"产品收藏");
        initRecycleView();
        getData();
    }

    private void initRecycleView(){
        cnxhEntityList=new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new SpaceItemDecoration(DensityUtil.dp2px(this,1)));
        baseQuickAdapter=new BaseQuickAdapter<BaokuanEntity>(R.layout.item_product_in,cnxhEntityList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, BaokuanEntity item) {
                    baseViewHolder.setText(R.id.tx_good_name,item.getProductName());
                baseViewHolder.setText(R.id.tx_good_price,item.getPrice());
                baseViewHolder.setText(R.id.tx_time,item.getTime());
                SimpleDraweeView simpleDraweeView=baseViewHolder.getView(R.id.sdv_list_item);
                FrescoUtil.loadNetUrl(simpleDraweeView,item.getImgUrl());
            }
        };
        baseQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getContext(),GoodDetailActivity.class);
                intent.putExtra("id",cnxhEntityList.get(position).getId());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(baseQuickAdapter);
    }

    private void getData(){
        enqueue(createCallService(HomeService.class).collectList(SePref.getToken()), new RetrofitCallbackListener<ColtResp>() {
            @Override
            public void onSuccess(Call<ColtResp> call, ColtResp result) {
                cnxhEntityList.clear();
                cnxhEntityList.addAll(result.getData());
                baseQuickAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }
}
